from datetime import datetime

from django.db import models
from django.utils.html import format_html
from apps.users.models import BaseModel
from apps.organizations.models import Teacher, CourseOrg


class Course(BaseModel):
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE, verbose_name="教师")
    coures_org = models.ForeignKey(CourseOrg,  null=True, blank=True, on_delete=models.CASCADE, verbose_name="课程机构")
    name = models.CharField(verbose_name="课程名", max_length=50)
    desc = models.CharField(verbose_name="课程描述", max_length=300)
    # 定义最小精度来存储
    learn_times = models.IntegerField(default=0,verbose_name="学习时长（分钟数）")
    degree = models.CharField(verbose_name="难度", choices=(("cj", "初级"), ("zj", "中级"), ("gj", "高级")),max_length=2)
    students = models.IntegerField(default=0,verbose_name='学习人数')
    fav_nums = models.IntegerField(default=0,verbose_name='收藏人数')
    click_nums = models.IntegerField(default=0, verbose_name='点击数')
    notice = models.CharField(verbose_name="课程公告", max_length=300,default='暂无公告')
    category = models.CharField(default=u"后端开发",max_length=20,verbose_name="课程类别")
    tag = models.CharField(default="", max_length=10, verbose_name="课程标签")
    youneed_know = models.CharField(default="", max_length=300, verbose_name="课程须知")
    teacher_tell = models.CharField(default="", max_length=300, verbose_name="老师告诉你")
    is_classics = models.BooleanField(default=False, verbose_name='经典课程')
    is_banner = models.BooleanField(default=False, verbose_name='是否广告')
    detail = models.TextField(verbose_name="课程详情")
    #实质上是CharField类型
    image = models.ImageField(upload_to="courses/%Y/%m",  null=True, blank=True, verbose_name="封面图", max_length=100)

    class Meta:
        verbose_name = "课程信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    def lesson_nums(self):
        return self.lesson_set.all().count()


    # 在xadmin中显示图片

    def show_image(self):
        return format_html(
            '<a href="/course/{}"> <img  src="{}"/></a>', self.id,self.image.url,)
    show_image.short_description = '图片'

class BannerCoures(Course):
    class Meta:
        verbose_name = "轮播课程"
        verbose_name_plural = verbose_name
        # 在做makration的时候不生成新表
        proxy = True

class CourseTag(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name='课程')
    tag = models.CharField(max_length=10, verbose_name=u'标签')

    class Meta:
        verbose_name = "课程标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag

class Lesson(BaseModel):
    # ForeignKey外键,on_delete指在链接的数据被删除后这条数据也会被删除,SET_NULL如果链接信息被删掉则这条数据被置为NULL
    course = models.ForeignKey(Course, on_delete=models.CASCADE,verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name=u'章节名')
    learn_time = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")

    class Meta:
        verbose_name = "课程章节"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Video(BaseModel):
    lesson = models.ForeignKey(Lesson, verbose_name="章节", on_delete=models.CASCADE)
    name = models.CharField(max_length=100, verbose_name=u"视频名")
    learn_time = models.IntegerField(default=0, verbose_name=u"学习时长(分钟数)")
    url = models.FileField(upload_to='courses/video/%Y/%m',verbose_name=u"访问地址")

    class Meta:
        verbose_name = "视频"
        verbose_name_plural = verbose_name


class CourseResource(BaseModel):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")
    name = models.CharField(max_length=100, verbose_name=u"名称")
    file = models.FileField(upload_to="course/resourse/%Y%m", verbose_name="下载地址", max_length=200)

    class Meta:
        verbose_name = "课程资源"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name